package com.zczc.tagmanagement.web.controller;


import com.github.pagehelper.PageInfo;
import com.yishuifengxiao.common.tool.collections.EmptyUtil;
import com.yishuifengxiao.common.tool.response.Response;
import com.yishuifengxiao.common.tool.response.Response.Const;
import com.yishuifengxiao.common.tool.validate.domain.Create;
import com.zczc.tagmanagement.dto.TagSortsDTO;
import com.zczc.tagmanagement.entity.TagSorts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 11:50 2018-12-11
 * @Modify by:
 */
@RestController
@RequestMapping("/zczc/tagsorts")
@Api(tags = "众筹众创标签分类维护")
@Validated
public class TagSortsController extends BaseController {


  private final static Logger log = LoggerFactory.getLogger(TagSortsController.class);

  @ApiOperation(value = "添加一条分类")
  @PostMapping("/addTagSorts")
  Response<String> addTagSorts(
      @ApiParam(value = "添加分类的名字", required = true) @RequestParam(required = true, value = "sortName")
      @NotEmpty(message = "sortName不能为空") String sortName) {
    TagSorts tagSorts = new TagSorts();
    if (sortName != null) {
      tagSorts.setSortName(sortName);
      try {
        tagSortsService.addTagSorts(tagSorts);
        return Response.suc();
      } catch (Exception e) {
        log.info(e.getMessage());
        return Response.error("失败");
      }
    }
    return Response.error("sortName参数不能为空");
  }

  @ApiOperation(value = "根据id选择性的修改某些字段")
  @PutMapping("/updateTagSorts")
  Response updateTagSorts(
      @ApiParam(required = true, value = "要修改的对象", name = "sortsDTO") @RequestBody(required = true) @Validated(Create.class) TagSortsDTO sortsDTO,
      BindingResult errors) {
    if (errors.hasErrors()) {
      return Response.badParam(errors.getAllErrors().get(0).getDefaultMessage());
    }

    try {
      tagSortsService.updateTagSortsById(sortsDTO);
    } catch (Exception e) {
      log.info(e.getMessage());
      return Response.error("修改失败");
    }
    return Response.suc();
  }

  @ApiOperation(value = "批量删除标签分类，逻辑删除")
  @DeleteMapping("deleteTagSortsByLogic")
  Response deleteTagSortsByLogic(
      @ApiParam(required = true, value = "要删除的id集合", name = "ids") @RequestBody(required = true) @NotEmpty(message = "参数不能为空") List<String> ids,
      BindingResult errors) {
    if (errors.hasErrors()) {
      return Response.badParam(errors.getAllErrors().get(0).getDefaultMessage());
    }
    try {
      tagSortsService.deleteTagSortsLogic(ids);
    } catch (Exception e) {
      log.info(e.getMessage());
      return Response.error("删除失败");
    }
    return Response.suc();
  }

  @ApiOperation(value = "分页查询及搜索")
  @GetMapping("/selectTagSortsByPage")
  Response<PageInfo<TagSorts>> selectTagSortsByPage(
      @ApiParam(required = false, value = "搜索的条件", name = "sortName") @RequestParam(required = false, value = "sortName") String sortName,
      @ApiParam(required = false, name = "pageSize", value = "页大小，默认为20", defaultValue = "20") @RequestParam(required = false, value = "pageSize", defaultValue = "20") @Min(value = 1, message = "页大小最小为1") Integer pageSize,
      @ApiParam(required = false, name = "pageNum", value = "页码", defaultValue = "1") @RequestParam(required = false, value = "pageNum", defaultValue = "1") @Min(value = 1, message = "页码最小为1") Integer pageNum) {
    TagSorts tagSorts = null;
    if (sortName != null) {
      tagSorts = new TagSorts();
      tagSorts.setSortName(sortName);
    }
    PageInfo<TagSorts> sortsPageInfo = tagSortsService
        .selectTagSortsByPage(tagSorts, pageSize, pageNum);
    return new Response<PageInfo<TagSorts>>(Const.CODE_OK, Const.MSG_OK, sortsPageInfo);
  }


}
